﻿using Demo.Domains.Entities.Sys;
using Demo.PetaPoco.Application.Dtos;
using Microsoft.Extensions.DependencyInjection;
using PmSoft.Core;
using PmSoft.Web.Abstractions.Attachment;

namespace Demo.PetaPoco.Application.Services.Attachment;

/// <summary>
/// 附件服务类，实现 IAttachmentService 接口，提供附件的创建、获取和发布功能。
/// </summary>
[Inject(ServiceLifetime.Scoped, typeof(IAttachmentService))]
public class AttachmentService(AttachmentRepository attachmentRepository) : IAttachmentService
{
	/// <summary>
	/// 异步创建附件。
	/// </summary>
	/// <param name="attachment">附件信息。</param>
	/// <returns>任务。</returns>
	public async Task CreateAsync(IAttachment attachment)
	{
		// 将 IAttachment 映射为 SysAttachment 实体
		var sysAttachment = attachment.MapTo<IAttachment, SysAttachment>(setter =>
		{
			setter.Map(m => m.Id, m => m.AttachmentId);
		});
		// 插入附件到数据库
		await attachmentRepository.InsertAsync(sysAttachment);
	}

	/// <summary>
	/// 删除附件
	/// </summary>
	/// <param name="attachmentId"></param>
	/// <returns></returns>
	public async Task DeleteAsync(string attachmentId)
	{
		await attachmentRepository.DeleteByEntityIdAsync(attachmentId);
	}

	/// <summary>
	/// 删除附件集合
	/// </summary>
	/// <param name="attachmentIds"></param>
	/// <returns></returns>
	public async Task DeleteBatchAsync(List<string> attachmentIds)
	{
		foreach (var attachmentId in attachmentIds)
			await DeleteAsync(attachmentId);
	}

	/// <summary>
	/// 异步获取附件信息。
	/// </summary>
	/// <param name="attachmentId">附件ID。</param>
	/// <returns>附件信息。</returns>
	/// <exception cref="ArgumentNullException">当附件不存在时抛出。</exception>
	public async Task<IAttachment> GetAsync(string attachmentId)
	{
		// 从数据库获取附件信息
		var sysAttachment = await attachmentRepository.GetAsync(attachmentId);
		if (sysAttachment == null)
			throw new ArgumentNullException("附件不存在");
		// 将 SysAttachment 映射为 AttachmentDto
		return sysAttachment.MapTo<SysAttachment, AttachmentOutDto>(setter =>
		{
			setter.Map(m => m.AttachmentId, m => m.Id);
		});
	}

	/// <summary>
	/// 异步批量获取附件信息
	/// </summary>
	/// <param name="attachmentIds"></param>
	/// <returns></returns>
	public async Task<IEnumerable<IAttachment>> GetBatchAsync(List<string> attachmentIds)
	{
		var sysAttachments = await attachmentRepository.GetEntitiesByIdsAsync(attachmentIds);
		return sysAttachments.Select(m => m.MapTo<SysAttachment, AttachmentOutDto>(setter =>
		{
			setter.Map(m => m.AttachmentId, m => m.Id);
		}));
	}

	/// <summary>
	/// 异步发布附件。
	/// </summary>
	/// <param name="attachment">附件信息。</param>
	/// <returns>任务。</returns>
	/// <exception cref="ArgumentNullException">当附件不存在时抛出。</exception>
	public async Task PublishAsync(IAttachment attachment)
	{
		// 从数据库获取附件信息
		var sysAttachment = await attachmentRepository.GetAsync(attachment.AttachmentId);
		if (sysAttachment == null)
			throw new ArgumentNullException("附件不存在");
		// 更新附件信息
		sysAttachment.TenantId = attachment.TenantId;
		sysAttachment.FriendlyName = attachment.FriendlyName;
		sysAttachment.ObjectName = attachment.ObjectName;
		sysAttachment.IsTemporary = false;
		// 更新附件到数据库
		await attachmentRepository.UpdateAsync(sysAttachment);
	}

	/// <summary>
	/// 异步批量发布附件
	/// </summary>
	/// <param name="updatedAttachments"></param>
	/// <returns></returns>
	public async Task PublishBatchAsync(List<IAttachment> updatedAttachments)
	{
		foreach (var attachment in updatedAttachments)
		{
			await PublishAsync(attachment);
		}
	}
}

